package com.dayi.view;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.dayi.R;
import com.lib.fastkit.views.recyclerview.tool.MyLinearLayoutManager;
import com.lib.fastkit.views.recyclerview.zhanghongyang.base.ViewHolder;
import com.lib.ui.adapter.BaseAdapter;

import java.util.List;

import androidx.recyclerview.widget.RecyclerView;

public class SpinnerPopWindow extends PopupWindow {

    private Context mContext;
    private RecyclerView mRvPopup;
    private SpinnerListAdapter mAdapter;
    List<String> dataList;

    public SpinnerPopWindow(Context context, List<String> dataList) {
        super(context);

        this.mContext = context;
        this.dataList = dataList;

        init();
    }


    private void init() {
        View view = LayoutInflater.from(mContext).inflate(R.layout.popup_spinner_recy, null);
        setContentView(view);
        setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
        setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);

        setFocusable(true);
        ColorDrawable dw = new ColorDrawable(0x00);
        setBackgroundDrawable(dw);

        mRvPopup = (RecyclerView) view.findViewById(R.id.rv_popup);

        mAdapter = new SpinnerListAdapter(mContext, dataList);
        MyLinearLayoutManager manager = new MyLinearLayoutManager(mContext);
        mRvPopup.setLayoutManager(manager);
        mRvPopup.setAdapter(mAdapter);

    }


    public class SpinnerListAdapter extends BaseAdapter<String> {

        private List<String> dateArr;

        public SpinnerListAdapter(Context context, List<String> mData) {
            super(context, mData);
            this.dateArr = mData;
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_spinner_layout;
        }

        @Override
        protected void toBindViewHolder(ViewHolder holder, final int position, final List<String> mData) {
            TextView textView = holder.getView(R.id.tv_grade);
            textView.setText(mData.get(position));


            textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (itemClickListener != null) {
                        itemClickListener.itemClick(mData.get(position));
                    }

                }
            });
        }

    }

    private SpinnerItemClickListener itemClickListener;

    public interface SpinnerItemClickListener {
        void itemClick(String itemText);
    }

    public void setSpinnerItemClickListener(SpinnerItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

}

